import 'package:flutter/material.dart';
import 'package:flutter_ncsjqz_getx/page/rong_im/util/rong_widget.dart';
import 'package:get/get.dart';

import 'chat_record_logic.dart';

/// <summary>
/// author：zwb
/// dateTime： 2021/12/15 17:41
/// filePath： lib/page/rong_im/chat_record_view.dart
/// desc: 聊天记录
/// <summary>
///
class ChatRecordPage extends StatefulWidget {
  final String? targetId;
  final int?  conversationType; // 1 单聊 3 群聊
  ChatRecordPage({this.targetId, this.conversationType});
  @override
  _ChatRecordPageState createState() => _ChatRecordPageState();
}

class _ChatRecordPageState extends State<ChatRecordPage> {
  final logic = Get.put(ChatRecordLogic());

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    logic.conversationType = widget.conversationType!;
    logic.targetId = widget.targetId;
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: RongWidget.appBarWidget(title: "查找聊天内容"),
      body: GetBuilder<ChatRecordLogic>(
        builder: (controller) {
          return Column(
            children: [
              headSearch(),

              Expanded(
                child: ListView.separated(
                  itemBuilder: (ctx,index){
                  return sessionItem(index);
                }, separatorBuilder: (ctx,index){
                  return Divider(color: Colors.grey.shade200,height: 0.5,);
                }, itemCount: logic.searchList.length,
                ),
              ),
            ],
          );
        },
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<ChatRecordLogic>();
    super.dispose();
  }

  /// 搜索栏
  Widget headSearch() {
    return Container(
      padding: EdgeInsets.all(15),
      child: RongWidget.rongInput(
        controller: logic.textEditingControllerSearch,
        circular: 5,
        height: 45,
        bgColor: Colors.white,
        prefixIcon: Icon(Icons.search,color: Colors.grey[400],),
        hintText: "输入聊天关键词",
        hintStyle: TextStyle(color: Colors.grey),
        onChanged: (v){
          if(v==""){
            logic.searchList = [];
            logic.update();
            return;
          }
          logic.search(false);
        },
      ),
    );
  }

  /// 会话列表->item
  Widget sessionItem(index) {
    return InkWell(
      onTap: (){
        print("进入聊天页面");
        // Get.to(()=>ChatPage(
        //   targetId: logic.contentList[index].targetId,
        //   title: logic.contentList[index].nickName,
        //   userPic: logic.contentList[index].cover,
        // ))!.then((value) => logic.getConversationList());
      },
      child: Container(
        color: Colors.white,
        padding: EdgeInsets.symmetric(horizontal: 15),
        child:  ListTile(
          leading: Stack(
            overflow: Overflow.visible,
            children: [
              /// 群聊头像
              // index == 1 ? Container(
              //   width: 60,
              //   height: 60,
              //   child: Wrap(
              //     runSpacing: 5,
              //     spacing: 5,
              //     children: List.generate(9, (index){
              //       return RongWidget.imgNet(RongApi.testImg1,width: (50 / 3),height: (50 / 3),borderRadius: BorderRadius.circular(5));
              //     }),
              //   ),
              // ) :
              RongWidget.imgNet("${logic.searchList[index].cover}",width: 60,height: 60,borderRadius: BorderRadius.circular(5)),
            ],
          ),
          title: RongWidget.title("${logic.searchList[index].nickName}"),
          subtitle: RongWidget.text("${logic.searchList[index].message}",color: Colors.grey.shade600),
          trailing: RongWidget.text("${logic.searchList[index].timer}",color: Colors.grey),
          contentPadding: EdgeInsets.symmetric(horizontal: 0,vertical: 5),
        ),
      ),
    );
  }
}